/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils.sql;

import java.net.URI;

import org.apache.log4j.Logger;

import net.groupme.model.ObjectFactory;
import net.groupme.model.User;
import net.groupme.model.cache.CachedObjectType;
import net.groupme.model.dao.DATABASE_CONSTANTS;

/**
 * created on Jul 26, 2007
 * 
 * This class provides some helper functions to query special stuff
 * from the cachet table (e.g. querying for cached tag clouds).
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: mischa $
 * 
 * @version $Revision: 1.3 $ $Date: 2007-10-23 17:10:03 $
 */
public class CacheQueryUtility {
	/** Logger for this class  */
	private static final Logger logger = Logger.getLogger(CacheQueryUtility.class);
	
	/**
	 * This method returns the id of the last record set within the 
	 * Cache table that has the given type.
	 * @param type the type of the cached object whose ID should be returned
	 * @return the id of the first record set within the Cache table that has the given type or {@link SelectQueryUtility#NO_RESULT_SET_FOUND} if no result set was found.
	 */
	public static final int getIdOfCachedObject(CachedObjectType type){
		String query = "SELECT " + DATABASE_CONSTANTS.CACHE_ID + " FROM " + DATABASE_CONSTANTS.TABLE_CACHE +
			" WHERE " + DATABASE_CONSTANTS.CACHE_OBJECT_TYPE + " = '" + type.getTypeValue() + "'";
		return SelectQueryUtility.getSingleIntFromTable(ObjectFactory.ds, query, DATABASE_CONSTANTS.CACHE_ID);
	}
	
	/**
	 * This method returns the id of the last record set within the 
	 * Cache table that has the given type and is associated with the given user.
	 * @param type the type of the cached object whose ID should be returned
	 * @param user user for whom the object is cached
	 * @return the id of the first record set within the Cache table that has the given type or {@link SelectQueryUtility#NO_RESULT_SET_FOUND} if no result set was found.
	 */
	public static final int getIdOfCachedObject(CachedObjectType type, User user){
		String query = "SELECT " + DATABASE_CONSTANTS.CACHE_ID + " FROM " + DATABASE_CONSTANTS.TABLE_CACHE +
			" WHERE " + DATABASE_CONSTANTS.CACHE_OBJECT_TYPE + " = '" + type.getTypeValue() + "'" + 
			"  AND " + DATABASE_CONSTANTS.CACHE_USER_ID + " = '" +  user.getId() + "'";
		logger.info("Querying for user's cached tag cloud id: " + query);
		return SelectQueryUtility.getSingleIntFromTable(ObjectFactory.ds, query, DATABASE_CONSTANTS.CACHE_ID);
	}
	
	/**
	 * This method returns the id of the last record set within the 
	 * Cache table that has the given type and is associated with the given context (identified via URI).
	 * @param type the type of the cached object whose ID should be returned
	 * @param relatedWith the URI of a context (e.g. group) to which the cached object belongs
	 * @return the id of the first record set within the Cache table that has the given type or {@link SelectQueryUtility#NO_RESULT_SET_FOUND} if no result set was found.
	 */
	public static final int getIdOfCachedObject(CachedObjectType type, URI relatedWith){
		String query = "SELECT " + DATABASE_CONSTANTS.CACHE_ID + " FROM " + DATABASE_CONSTANTS.TABLE_CACHE +
			" WHERE " + DATABASE_CONSTANTS.CACHE_OBJECT_TYPE + " = '" + type.getTypeValue() + "'" +
			"  AND " + DATABASE_CONSTANTS.CACHE_RELATED_WITH_URI + " = '" +  relatedWith + "'";
		return SelectQueryUtility.getSingleIntFromTable(ObjectFactory.ds, query, DATABASE_CONSTANTS.CACHE_ID);
	}
	
	/**
	 * This method returns the id of the last record set within the 
	 * Cache table that has the given type and is associated with the given user and the given context (identified via URI).
	 * @param type the type of the cached object whose ID should be returned
	 * @param user user for whom the object is cached
	 * @param relatedWith the URI of a context (e.g. group) to which the cached object belongs
	 * @return the id of the first record set within the Cache table that has the given type or {@link SelectQueryUtility#NO_RESULT_SET_FOUND} if no result set was found.
	 */
	public static final int getIdOfCachedObject(CachedObjectType type, User user, URI relatedWith){
		String query = "SELECT " + DATABASE_CONSTANTS.CACHE_ID + " FROM " + DATABASE_CONSTANTS.TABLE_CACHE +
			" WHERE " + DATABASE_CONSTANTS.CACHE_OBJECT_TYPE + " = '" + type.getTypeValue() + "'" +
			"  AND " + DATABASE_CONSTANTS.CACHE_USER_ID + " = '" +  user.getId() + "'" +
			"  AND " + DATABASE_CONSTANTS.CACHE_RELATED_WITH_URI + " = '" +  relatedWith + "'";
		return SelectQueryUtility.getSingleIntFromTable(ObjectFactory.ds, query, DATABASE_CONSTANTS.CACHE_ID);
	}

}
